package cn.zhqr.common.util.io;

import java.io.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class PropUtil {
    private static final ConcurrentHashMap<String, Prop> propMap = new ConcurrentHashMap();
    private static Prop curUseProp = null;

    public static Prop use(String fileName) {
        return use(fileName,"UTF-8");
    }

    public static Prop use(String fileName,String charset) {
        Prop props = propMap.get(fileName);
        if(props == null){
            props = new Prop(fileName,charset);
            if (curUseProp == null) {
                curUseProp = props;
            }
        }
        return props;
    }

    private static InputStream loadFile(String fileName){
        InputStream in = null;
        try {
            in = new FileInputStream(fileName);
        } catch (FileNotFoundException var7) {
            in = PropUtil.class.getClassLoader().getResourceAsStream(fileName);
        }
        return in;
    }

    public static boolean exist(String fileName){
        return loadFile(fileName)!=null;
    }

    public static Prop getProp(){
        if(curUseProp == null){
            throw new IllegalStateException("Load properties file by invoking PropUtil.use(String propertyFileName) method first.");
        }
        return curUseProp;
    }

    public static Short getShort(String key, Short defaultValue) {
        return getProp().getShort(key,defaultValue);
    }
    public static Short getShort(String key) {
        return getProp().getShort(key);
    }

    public static Integer getInt(String key, Integer defaultValue) {
        return getProp().getInt(key,defaultValue);
    }

    public static Integer getInt(String key) {
        return getProp().getInt(key);
    }

    public static Long getLong(String key, Long defaultValue) {
        return getProp().getLong(key,defaultValue);
    }
    public static Long getLong(String key) {
        return getProp().getLong(key);
    }

    public static Float getFloat(String key, Float defaultValue) {
        return getProp().getFloat(key,defaultValue);
    }
    public static Float getFloat(String key) {
        return getProp().getFloat(key);
    }

    public static Double getDouble(String key, Double defaultValue) {
        return getProp().getDouble(key,defaultValue);
    }
    public static Double getDouble(String key) {
        return getProp().getDouble(key);
    }

    public static String getStr(String key, String defaultValue) {
        return getProp().getStr(key,defaultValue);
    }

    public static String getStr(String key) {
        return getProp().getStr(key);
    }

    public static Boolean getBool(String key, Boolean defaultValue) {
        return getProp().getBool(key,defaultValue);
    }

    public static Boolean getBool(String key) {
        return getProp().getBool(key);
    }


    public static class Prop {
        private Properties properties;

        public Prop(String fileName) {
            this(fileName, "UTF-8");
        }

        public Prop(String fileName, String charset) {
            this.properties = null;
            InputStream inputStream = null;

            try {
                inputStream = loadFile(fileName);
                if (inputStream == null) {
                    throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
                }

                this.properties = new Properties();
                this.properties.load(new InputStreamReader(inputStream, charset));
            } catch (IOException var12) {
                throw new RuntimeException("Error loading properties file.", var12);
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException var11) {
                        var11.printStackTrace();
                    }
                }

            }

        }

        public Prop(File file) {
            this(file, "UTF-8");
        }

        public Prop(File file, String encoding) {
            this.properties = null;
            if (file == null) {
                throw new IllegalArgumentException("File can not be null.");
            } else if (!file.isFile()) {
                throw new IllegalArgumentException("File not found : " + file.getName());
            } else {
                FileInputStream inputStream = null;

                try {
                    inputStream = new FileInputStream(file);
                    this.properties = new Properties();
                    this.properties.load(new InputStreamReader(inputStream, encoding));
                } catch (IOException var12) {
                    throw new RuntimeException("Error loading properties file.", var12);
                } finally {
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException var11) {
                            var11.printStackTrace();
                        }
                    }

                }

            }
        }

        public String getStr(String key) {
            return this.properties.getProperty(key);
        }

        public String getStr(String key, String defaultValue) {
            return this.properties.getProperty(key, defaultValue);
        }

        public Short getShort(String key) {
           return this.getShort(key,null);
        }

        public Short getShort(String key,Short defaultValue) {
            String value = this.properties.getProperty(key);
            return value != null && value.trim().length()>0? Short.parseShort(value.trim()) : defaultValue;
        }

        public Integer getInt(String key) {
            return this.getInt(key, null);
        }

        public Integer getInt(String key, Integer defaultValue) {
            String value = this.properties.getProperty(key);
            return value != null && value.trim().length()>0 ? Integer.parseInt(value.trim()) : defaultValue;
        }

        public Long getLong(String key) {
            return this.getLong(key, null);
        }

        public Long getLong(String key, Long defaultValue) {
            String value = this.properties.getProperty(key);
            return value != null && value.trim().length()>0? Long.parseLong(value.trim()) : defaultValue;
        }

        public Boolean getBool(String key) {
            return this.getBool(key, null);
        }

        public Boolean getBool(String key, Boolean defaultValue) {
            String value = this.properties.getProperty(key);
            if (value != null) {
                value = value.toLowerCase().trim();
                if ("true".equals(value)) {
                    return true;
                } else if ("false".equals(value)) {
                    return false;
                } else {
                    throw new RuntimeException("The value can not parse to Boolean : " + value);
                }
            } else {
                return defaultValue;
            }
        }

        public Float getFloat(String key) {
            return this.getFloat(key,null);
        }

        public Float getFloat(String key,Float defaultValue) {
            String value = this.properties.getProperty(key);
            return value != null && value.trim().length()>0? Float.parseFloat(value.trim()) : defaultValue;
        }

        public Double getDouble(String key) {
            return this.getDouble(key,null);
        }

        public Double getDouble(String key,Double defaultValue) {
            String value = this.properties.getProperty(key);
            return value != null && value.trim().length()>0? Double.parseDouble(value.trim()) : defaultValue;
        }

        public boolean containsKey(String key) {
            return this.properties.containsKey(key);
        }

        public Properties getProperties() {
            return this.properties;
        }
    }
}
